﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YJKArchUtils.Utils;
using YJKRevitView;

namespace YJKRevitColumn.ColumnModule
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
    public class CmdColumnInsertOld : YJKCommandManual
	{
        protected override Result YJKExcute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			CmdViewType types = (CmdViewType)3;
			if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			Document document = cmdData.Application.ActiveUIDocument.Document;
			if (document.ActiveView is ViewPlan && document.ActiveView.SketchPlane == null)
			{
				Plane plane = RevitVersionFuncs.CreatePlanByOriginAndBasis(document.ActiveView.Origin, document.ActiveView.RightDirection, document.ActiveView.UpDirection);
				Transaction transaction = new Transaction(document, "SketchPlane");
				transaction.Start();
				SketchPlane sketchPlane = RevitVersionFuncs.NewSketchPlane(document, plane);
				document.ActiveView.SketchPlane = sketchPlane;
				transaction.Commit();
			}
			Level currentViewLevel = AssistFunc.GetCurrentViewLevel(this.m_doc);
			Result result;
			try
			{
				using (ColumnInsert columnInsert = new ColumnInsert(this.m_uiApp))
				{
					try
					{
						IL_A8:
						while (columnInsert.ShowDialog() == DialogResult.OK)
						{
							ColumnDatum columnDatum = new ColumnDatum();
							columnDatum.level = currentViewLevel;
							ColumnCreator columnCreator = new ColumnCreator(this.m_uiApp, columnDatum);
							for (;;)
							{
								int insertType = columnInsert.InsertType;
								List<XYZ> list = new List<XYZ>();
								if (insertType == 0)
								{
									if (columnCreator.GetPointPts(ref list) != null && columnInsert.PickButton)
									{
										break;
									}
								}
								else if (insertType == 1)
								{
									if (columnCreator.GetAxisPts(ref list) != null && columnInsert.PickButton)
									{
										goto Block_14;
									}
								}
								else if (insertType == 2 && columnCreator.GetRectPts(ref list) != null && columnInsert.PickButton)
								{
									goto Block_17;
								}
								columnDatum.colSym = columnInsert.GetColumnFamilySymbol();
								if (columnDatum.colSym == null)
								{
									goto IL_A8;
								}
								columnDatum.bottomOffset = columnInsert.BottomOffset;
								columnDatum.topOffset = columnInsert.TopOffset;
								columnDatum.xOffset = columnInsert.XOffset;
								columnDatum.yOffset = columnInsert.YOffset;
								columnDatum.angle = columnInsert.Angle;
								columnDatum.autoRotate = columnInsert.AutoRotate;
								columnDatum.autoRotateWall = columnInsert.AutoRotateWall;
								columnDatum.anchorPoint = columnInsert.index;
								columnDatum.width = columnInsert.width;
								columnDatum.depth = columnInsert.depth;
								if (!Geometry.Lessthan_Or_Equal((this.m_doc.GetElement(columnInsert.TopLevelId) as Level).Elevation, currentViewLevel.Elevation))
								{
									columnDatum.idTopLevel = columnInsert.TopLevelId;
									columnDatum.ConvertUnit(true);
									columnCreator.SetColumnDatum(columnDatum);
									using (List<XYZ>.Enumerator enumerator = list.GetEnumerator())
									{
										while (enumerator.MoveNext())
										{
											XYZ pt = enumerator.Current;
											Transaction transaction2 = null;
											try
											{
												transaction2 = new Transaction(this.m_doc, "insertcol");
												transaction2.Start();
												if (!columnCreator.InsertColumn(pt, columnInsert.IsArchColumn))
												{
													if (transaction2 != null && transaction2.HasStarted() && !transaction2.HasEnded())
													{
														transaction2.RollBack();
													}
												}
												else
												{
													this.m_doc.Regenerate();
													transaction2.Commit();
												}
											}
											catch (Exception)
											{
												if (transaction2 != null && transaction2.HasStarted() && !transaction2.HasEnded())
												{
													transaction2.RollBack();
												}
											}
										}
										continue;
									}
									goto IL_30C;
								}
								YJKMessageBox.Show("所选顶部标高高程<=当前标高高程，请重新设置顶标高");
							}
							columnInsert.PickButton = false;
							continue;
							Block_14:
							columnInsert.PickButton = false;
							continue;
							Block_17:
							columnInsert.PickButton = false;
						}
						IL_30C:
						columnInsert.CloseDocuments();
					}
					catch (Exception ex)
					{
						YJKMessageBox.Show(ex.Message);
					}
					result = base.YJKExcute(cmdData, ref message, elements);
				}
			}
			catch (Exception ex2)
			{
				YJKMessageBox.Show(ex2.Message);
                result = base.YJKExcute(cmdData, ref message, elements);
			}
			return result;
		}
	}
}
